﻿import win32gui


def enum_child_proc(hwnd, lParam):
    class_name_to_find = lParam[0]
    title_to_find = lParam[1]

    if (class_name_to_find is None
            or win32gui.GetClassName(hwnd) == class_name_to_find):
        if (title_to_find is None
                or win32gui.GetWindowText(hwnd) == title_to_find):
            lParam[2] = hwnd  # 找到匹配的子窗口时，通过lParam传递结果
            return False  # 停止进一步的枚举


def find_child_window(parent_hwnd, class_name=None, window_title=None):
    # 初始化lParam作为结果存储
    lParam = [class_name, window_title, None]

    # 枚举子窗口，并将结果存储在lParam中
    win32gui.EnumChildWindows(parent_hwnd, enum_child_proc, lParam)

    return lParam[2]  # 返回找到的子窗口句柄，如果没有找到则返回None


# 使用示例：
def FindChildWindow(main_title, class_name=None, window_title=None):
    parent_hwnd = win32gui.FindWindow(None, main_title)  # 替换为你想要找子窗口的主窗口标题
    child_hwnd = find_child_window(parent_hwnd,
                                   class_name=None,
                                   window_title=window_title)

    if child_hwnd:
        print(f"找到了子窗口句柄: {child_hwnd}")
    else:
        print("未找到指定的子窗口")


if __name__ == "__main__":
    FindChildWindow(main_title=r"InTouch - WindowViewer - C:\PROGRAMDATA\ARCHESTRA\MANAGEDAPP", window_title="刘老涧二站实时报表页面")
